#include "ScoreLayer.h"
#include <iostream>
#include <sstream>
#include "GameOverScene.h"
using namespace cocos2d;

ScoreLayer* ScoreLayer::node() {
	ScoreLayer *layer = ScoreLayer::create();
	return layer;
}

bool ScoreLayer::init() {
	if (!CCLayer::init()) {
		return false;
	}

	scoreLabel = CCLabelTTF::create("0", "Thonburi", 34);
	livesLabel = CCLabelTTF::create("3", "Thonburi", 34);
	CCSize labelSize = scoreLabel->getContentSize();
	CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
	scoreLabel->setPosition(
			CCPointMake(labelSize.width, screenSize.height-labelSize.height));
	this->addChild(scoreLabel, 0, 0);

	labelSize = livesLabel->getContentSize();
	livesLabel->setPosition(
			CCPointMake(screenSize.width/2, screenSize.height-labelSize.height));
	this->addChild(livesLabel, 0, 0);

	return true;
}
void ScoreLayer::addPoint() {
	const char * currentText = scoreLabel->getString();
	int currentX = atoi(currentText);
	currentX = currentX + 1;
	if (currentX > 100) {
		currentX = 1;
	}
	char * n = (char *) malloc(sizeof(char));
	sprintf(n, "%d", currentX);
	scoreLabel->setString(n);
}
void ScoreLayer::takeLive() {
	const char * currentText = livesLabel->getString();
	int currentX = atoi(currentText);
	currentX = currentX - 1;
	if (currentX < 0) {
		CCLog("!!!GameOver!!!");
		renderEndScreen(false);
	}
	char * n = (char *) malloc(sizeof(char));
	sprintf(n, "%d", currentX);
	livesLabel->setString(n);
}
void ScoreLayer::won() {
	renderEndScreen(true);
}
void ScoreLayer::renderEndScreen(bool won) {
	CCScene *gv = GameOverScene::scene(won);
	CCDirector::sharedDirector()->replaceScene(gv);
}
